#include "keyboard.h"
#include "main.h"
#include "usb_device.h"
#include "usbd_hid.h"
#include "oled.h"
//extern char SendingPackage[8];
/*************************************************************/
//Default_mode = 0
//User_mode = 1
//IsRead (default) = 0
/*************************************************************/
extern uint8_t IsRead;
extern uint8_t group;  //修改第x组，共三组，

void KeyPackageSend(void);
void KeyPackageClear(uint8_t SendingPackage[]);
uint8_t KEYPAD8_Read (uint8_t SendingPackage[]);
uint8_t KEYPAD8_COSTOMIZE_Read (uint8_t SendingPackage[],uint8_t CostomizeKeyValue[],uint8_t IsSpecialKey[]);		//键盘处理函数
//键盘处理函数

extern USBD_HandleTypeDef hUsbDeviceFS;


extern uint8_t selectedgroup;
extern uint8_t Costomize_KeyValue[3][8];
extern uint8_t Costomize_IsSpecialKey[3][8];
extern uint8_t Costomize_KeyID[3][8];

//uint8_t Space_Hold ;
//uint8_t Z_Hold;
//uint8_t X_Hold;
//uint8_t Up_Hold;
//uint8_t Left_Hold;
//uint8_t Down_Hold;
//uint8_t Right_Hold;

void KeyPackageSend(void)
{
	uint8_t Package[8]={0x00,0,0,0,0,0,0,0};
	//pPackage = Package;
//	uint8_t Costomize[8]={0x02,0x01,0x1d,0x1b,0x52,0x50,0x51,0x4f};
//	uint8_t Special[8]={1,1,0,0,0,0,0,0};
	if(selectedgroup==0)//默认
	{
		if(KEYPAD8_Read(Package))
		{
			USBD_HID_SendReport(&hUsbDeviceFS, Package, 8);
			KeyPackageClear(Package);
			IsRead = 1;
		}
		else if(IsRead)
		{
			USBD_HID_SendReport(&hUsbDeviceFS, Package, 8);
			IsRead = 0;
		}
	}
	else
	{
		if(KEYPAD8_COSTOMIZE_Read(Package, Costomize_KeyValue[selectedgroup-1], Costomize_IsSpecialKey[selectedgroup-1]))//测试后要修改
		{
			USBD_HID_SendReport(&hUsbDeviceFS, Package, 8);
			KeyPackageClear(Package);
			IsRead = 1;
		}
		else if(IsRead)
		{
			USBD_HID_SendReport(&hUsbDeviceFS, Package, 8);
			IsRead = 0;
		}
	}

}

void KeyPackageClear(uint8_t SendingPackage[])
{
	SendingPackage[0]=0x00;
	SendingPackage[1]=0x00;
	SendingPackage[2]=0x00;
	SendingPackage[3]=0x00;
	SendingPackage[4]=0x00;
	SendingPackage[5]=0x00;
	SendingPackage[6]=0x00;
	SendingPackage[7]=0x00;
}

uint8_t KEYPAD8_Read (uint8_t SendingPackage[])		//键盘处理函数
{
	uint8_t count=2;
	//extern SendingPackage[];
	if(!HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin)
			||!HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)
			||!HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)
			||!HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin)
			||!HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin)
			||!HAL_GPIO_ReadPin(KEY5_GPIO_Port, KEY5_Pin)
			||!HAL_GPIO_ReadPin(KEY6_GPIO_Port, KEY6_Pin)
			||!HAL_GPIO_ReadPin(KEY7_GPIO_Port, KEY7_Pin)
		) 	//查寻键盘口的值是否变化
	{
		HAL_Delay(10);//延时10毫秒
		KeyPackageClear(SendingPackage);
		uint8_t Shift_Hold = ! HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin);
		uint8_t Space_Hold = ! HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
		uint8_t Z_Hold = ! HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
		uint8_t X_Hold = ! HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin);
		uint8_t Up_Hold = ! HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin);
		uint8_t Left_Hold = ! HAL_GPIO_ReadPin(KEY5_GPIO_Port, KEY5_Pin);
		uint8_t Down_Hold = ! HAL_GPIO_ReadPin(KEY6_GPIO_Port, KEY6_Pin);
		uint8_t Right_Hold = ! HAL_GPIO_ReadPin(KEY7_GPIO_Port, KEY7_Pin);

		if(Shift_Hold)
		{
			SendingPackage[0]=Shift_KeyValue;
		}
		if( Z_Hold || Space_Hold || X_Hold || Up_Hold|| Left_Hold || Down_Hold || Right_Hold )
		{
			if(Space_Hold) SendingPackage[count++]=Space_KeyValue;
			if(Z_Hold && count<8) SendingPackage[count++]=Z_KeyValue;
			if(X_Hold && count<8) SendingPackage[count++]=X_KeyValue;
			if(Up_Hold && count<8) SendingPackage[count++]=Up_KeyValue;
			if(Left_Hold && count<8) SendingPackage[count++]=Left_KeyValue;
			if(Down_Hold && count<8) SendingPackage[count++]=Down_KeyValue;
			if(Right_Hold && count<8) SendingPackage[count++]=Right_KeyValue;
			count=2;
		}
		return 0x01;
	}
	else
		return 0x00;
}
uint8_t  KEYPAD8_COSTOMIZE_Read(uint8_t SendingPackage[],uint8_t CostomizeKeyValue[],uint8_t IsSpecialKey[])		//键盘处理函数
{
	uint8_t count=2;
	//extern SendingPackage[];
	if(!HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin)
			||!HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)
			||!HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)
			||!HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin)
			||!HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin)
			||!HAL_GPIO_ReadPin(KEY5_GPIO_Port, KEY5_Pin)
			||!HAL_GPIO_ReadPin(KEY6_GPIO_Port, KEY6_Pin)
			||!HAL_GPIO_ReadPin(KEY7_GPIO_Port, KEY7_Pin)
		) 	//查寻键盘口的值是否变化
	{
		HAL_Delay(10);//延时10毫秒
		KeyPackageClear(SendingPackage);
		uint8_t KEY0_Hold = ! HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin);
		uint8_t KEY1_Hold = ! HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
		uint8_t KEY2_Hold = ! HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
		uint8_t KEY3_Hold = ! HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin);
		uint8_t KEY4_Hold = ! HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin);
		uint8_t KEY5_Hold = ! HAL_GPIO_ReadPin(KEY5_GPIO_Port, KEY5_Pin);
		uint8_t KEY6_Hold = ! HAL_GPIO_ReadPin(KEY6_GPIO_Port, KEY6_Pin);
		uint8_t KEY7_Hold = ! HAL_GPIO_ReadPin(KEY7_GPIO_Port, KEY7_Pin);

		if( KEY0_Hold || KEY1_Hold || KEY2_Hold || KEY3_Hold || KEY4_Hold || KEY5_Hold || KEY6_Hold || KEY7_Hold )
		{
			if(KEY0_Hold)
			{
				if(!IsSpecialKey[0] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[0];
				}
				if(IsSpecialKey[0])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[0];
				}
			}
			if(KEY1_Hold)
			{
				if(!IsSpecialKey[1] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[1];
				}
				if(IsSpecialKey[1])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[1];
				}
			}
			if(KEY2_Hold)
			{
				if(!IsSpecialKey[2] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[2];
				}
				if(IsSpecialKey[2])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[2];
				}
			}
			if(KEY3_Hold)
			{
				if(!IsSpecialKey[3] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[3];
				}
				if(IsSpecialKey[4])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[3];
				}
			}
			if(KEY4_Hold)
			{
				if(!IsSpecialKey[4] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[4];
				}
				if(IsSpecialKey[4])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[4];
				}
			}
			if(KEY5_Hold)
			{
				if(!IsSpecialKey[5] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[5];
				}
				if(IsSpecialKey[5])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[5];
				}
			}
			if(KEY6_Hold)
			{
				if(!IsSpecialKey[6] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[6];
				}
				if(IsSpecialKey[6])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[6];
				}
			}
			if(KEY7_Hold)
			{
				if(!IsSpecialKey[7] && count<8)//非特殊键且累计不超过6，之间入包
				{
					SendingPackage[count++]=CostomizeKeyValue[7];
				}
				if(IsSpecialKey[7])//特殊键与第一位与运算
				{
					SendingPackage[0] |= CostomizeKeyValue[7];
				}
			}


//			if(Space_Hold) SendingPackage[count++]=Space_KeyValue;
//			if(Z_Hold && count<8) SendingPackage[count++]=Z_KeyValue;
//			if(X_Hold && count<8) SendingPackage[count++]=X_KeyValue;
//			if(Up_Hold && count<8) SendingPackage[count++]=Up_KeyValue;
//			if(Left_Hold && count<8) SendingPackage[count++]=Left_KeyValue;
//			if(Down_Hold && count<8) SendingPackage[count++]=Down_KeyValue;
//			if(Right_Hold && count<8) SendingPackage[count++]=Right_KeyValue;
			count=2;
		}
		return 0x01;
	}
	else
		return 0x00;
}
